package com.jgdt.deploy;

import cn.hutool.json.JSONObject;
import com.jgdt.utils.EsUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @author wangbin
 */
@Service
@Slf4j
public class DeployService {
    public static final String DEPLOY_PROJECT = "deploy-project";
    private @Resource EsUtils esUtils;

    public Mono<ServerResponse> list(ServerRequest ignore) {
        return esUtils.list(DEPLOY_PROJECT);
    }

    public Mono<ServerResponse> save(ServerRequest serverRequest) {
        return serverRequest.bodyToMono(JSONObject.class)
                .flatMap(s -> esUtils.save(DEPLOY_PROJECT, s));
    }

    public Mono<ServerResponse> delete(ServerRequest serverRequest) {
        String id = serverRequest.pathVariable("id");
        return esUtils.delete(DEPLOY_PROJECT, id);
    }
}
